﻿import QtQuick


Item{
    id:root
    clip: true
    property alias text: t1.text
    property alias color: t1.color
    property alias font: t1.font
    property real speed: 40 //像素/s
    property int startDelay: 800 //开始前停顿
    property bool needScroll: t1.contentWidth > root.width
    property int gap: 50 //歌曲动画文字移动间隔
    implicitHeight: Math.ceil(t1.contentHeight)   // 用文本的内容高做组件的隐式高
    height: implicitHeight

    Row{
        id:rr
        x:0
        anchors.verticalCenter: parent.verticalCenter
        spacing: root.gap
        Text{
            id:t1
        }

        Text {
            id: t2
            text: t1.text
            color: t1.color
            font:t1.font
            visible: needScroll ? true : false

        }

        SequentialAnimation{
            id:ani0


            PauseAnimation{

                duration: startDelay
            }

            ScriptAction { script: ani.start() }         // 启动真正的循环
        }

        SequentialAnimation{
            id:ani
            loops: Animation.Infinite


            NumberAnimation{
                target: rr
                property: "x"
                to: -(t1.paintedWidth + root.gap)
                duration: Math.max(1,
                         (t1.paintedWidth + root.gap) / root.speed * 1000)
            }
            //重置状态
            PropertyAction { target: rr; property: "x"; value: 0 }
        }
    }

    onWidthChanged: if (ani0.running) ani0.restart()
    Connections {
        target: t1
        function onContentWidthChanged() { if (ani0.running) ani0.restart() }
        function onTextChanged()         { if (ani0.running) ani0.restart() }
        function onFontChanged()         { if (ani0.running) ani0.restart() }
    }

    Component.onCompleted: {
        if(needScroll)    ani0.start()
    }


}
